package com.maycurobj.service.convert;

import com.maycur.developer.sdk.maycur.ultimate.openapi.entity.*;
import com.maycur.developer.sdk.maycur.ultimate.openapi.request.PostReceiveApplicationRequest;
import com.maycurobj.domain.maycurDTO.BpmItineraryDTO;
import com.maycurobj.domain.maycurDTO.TravelPartner;
import com.maycurobj.entity.bpm.JobNumberDTO;
import com.maycurobj.service.impl.MaycurService;
import com.maycurobj.util.NormalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * bpm的行程对象转为 maycur的行程对象
 */
@Service
@Slf4j
public class BpmTravelToMC {

    @Autowired
    private MaycurService maycurService;

    private TravelPartnerInfoEntity internalTravelPartnerEntity = new TravelPartnerInfoEntity();

    /**
     * bpm 转 maycur
     *
     * PostReceiveApplicationRequest 为 每刻sdk的对象
     * @param dto
     */
    public PostReceiveApplicationRequest bpmToMC(BpmItineraryDTO dto) {
        //OPDCA申请单对象
        PostReceiveApplicationRequest request = new PostReceiveApplicationRequest();
        //行程对象
        TravelRouteInputEntity travelRouteInputEntity = new TravelRouteInputEntity();
        //行程地址
        TravelRouteInputEntity.TravelCities travelCities = new TravelRouteInputEntity.TravelCities();
        //行程出发 需要转换
        travelCities.setDeparture(dto.getFromCityId());
        if(!Objects.isNull(dto.getToCityIds())){
            travelCities.setDestination(dto.getToCityIds());
        }else{
            travelCities.setDestination(Arrays.asList(dto.getToCityId()));
        }
        //行程时间 的 处理
        String _startTime = NormalUtil.longTimeToShortTime(dto.getNew_date_start()) + " " + apmToTime(dto.getStart_time());
        String _endTime = NormalUtil.longTimeToShortTime(dto.getNew_date()) + " " + apmToTime(dto.getEnd_time());
        TravelRouteInputEntity.TravelTimeRange travelTimeRange = new TravelRouteInputEntity.TravelTimeRange();
        DateSubEntity startDate=new DateSubEntity();
        DateSubEntity endDate = new DateSubEntity();
        startDate.setCurrentTime(NormalUtil.dateTimeMsToLong(_startTime));
        endDate.setCurrentTime(NormalUtil.dateTimeMsToLong(_endTime));
        travelTimeRange.setStartDate(startDate);
        travelTimeRange.setEndDate(endDate);
        //自定义 字段
        Map<String, Object> customMap = new HashMap<>();
        //importFlag
        customMap.put("CF214","import");
        customMap.put("CF245",dto.getNewFormCode());
        //出差说明 (240408 改为固定 词语)
//        customMap.put("CF201", handleStringLength(dto.getNew_work_summary()));
        customMap.put("CF201", "因公出差");
        //客户名称
        customMap.put("CF208", dto.getNew_account_name());
        Map<String, Object> _urlMap = new HashMap<>();
        String _url = dto.getUrl() + dto.getObjId();
        _urlMap.put("hyperlinkAddress", _url);
        _urlMap.put("hyperlinkName", "点击查看OPDCA详情");
        //OPDCA 申请单url 需要拼接
        customMap.put("CF209", _urlMap);
        //自定义字段
        travelRouteInputEntity.setCustomObject(customMap);
        //行程类型 需转换
        travelRouteInputEntity.setTripWay(enumTripWay(dto.getTripType()));
        travelRouteInputEntity.setTravelCities(travelCities);
        travelRouteInputEntity.setTravelTimeRange(travelTimeRange);
        //出差天数(如果不传入，没有出差日期 )
        travelRouteInputEntity.setTravelDays(dto.getBusiness_trip_days());
//        List<InternalTravelPartnerEntity> list = new ArrayList<>();
//        for (JobNumberDTO list1:dto.getJobNumber()){
//            InternalTravelPartnerEntity travelPartnerInfo = new InternalTravelPartnerEntity();
//            travelPartnerInfo.setEmployeeId(list1.getEmployeeNo());
//            list.add(travelPartnerInfo);
//
//        }
//        internalTravelPartnerEntity.setInternalTravelPartner(list);
//
//        request.setTravelPartnerInfo(internalTravelPartnerEntity);
        /**
         * -----------------------
         * 整理主表
         */

        request.setTravelRouteList(Arrays.asList(travelRouteInputEntity));

        //出差原因
        request.setPreConsumeName(StringUtils.isBlank(dto.getSubject())?dto.getNew_account_name()+"访客计划":dto.getSubject());
        //申请单业务编号（固定）FT61371951729350755
        request.setFormSubTypeBizCode("FT61371951729350755");
        //自定义字段 预计金额
        Map<String, Object> _appliedAmount = new HashMap<>();
        _appliedAmount.put("currency", "CNY");
        _appliedAmount.put("amount", dto.getPlanAmount().toString());
        Map<Object, Object> _customObject = new HashMap<>();
        request.setCustomObject(_customObject);
        Map<String, Object> _appliedAmoun2 = new HashMap<>();
        _appliedAmoun2.put("currencyCode", "CNY");
        _appliedAmoun2.put("amount", dto.getPlanAmount().toString());
        //自定义金额
        _customObject.put("CF210", _appliedAmount);
        //申请金额
        request.setAppliedAmount(_appliedAmoun2);
        request.setSubmittedTime(NormalUtil.dateTimeMsToLong(_startTime));
        request.setComment(dto.getNew_work_summary());
        request.setRequestUserEmpNo(dto.getEmpNo());
        //承担部门 需要转换(用 承担人)
        request.setDepartmentBizCode(getMaycurDepartCode(dto.getEmpNo()));
        request.setFormCode(dto.getNewFormCode());
        //转换完成
        return request;

    }

    /**
     * 单程or 往返转换
     * @param s
     * @return
     */
    private String enumTripWay(String s){
        if(s.equals("单程")){
            return "SINGLE";
        }else{
            return "ROUND";
        }
    }

    public String copyFrom(JobNumberDTO dto) {


        return dto.getEmployeeNo();
    }

    private String getMaycurDepartCode(String code){
        return maycurService.getEmployeeDetailsToDept(code, null);
    }

    private String apmToTime(String s) {
        switch (s) {
            case "上午":
                return "00:00:00 000";
            case "下午":
                return "23:59:59 999";
            default:
                return null;
        }
    }

    private String handleStringLength(String s) {
        try {
            if (s.length() > 130) {
                return s.substring(0, 129);
            }
            return s;
        } catch (Exception e) {
            return s.substring(0,100);
        }
    }

    public static void main(String[] args) {
        String s = "2023-12-27 11:11:11";
        String apm = "下午";
        String shortTime = NormalUtil.longTimeToShortTime(s);
        String ret = shortTime + " " + new BpmTravelToMC().apmToTime(apm);
        long ret_long = NormalUtil.dateTimeMsToLong(ret);
        System.out.println("---------->>>"+ret_long);

    }
}
